﻿using BW.MLTT.Domain.DTO;
using BW.MLTT.Domain.Entity;
using BW.MLTT.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using System;
using System.Diagnostics;
using Yitter.IdGenerator;

namespace BW.MLTT.API.Controllers
{
    /// <summary>
    /// API控制器-商品
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GoodController : ControllerBase
    {
        private readonly IGoodService service;

        public GoodController(IGoodService service)
        {
            this.service = service;
        }
    
        /// <summary>
        /// 获取雪花ID
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetYitter()
        {
            var YitterId=YitIdHelper.NextId().ToString();
            return Ok(YitterId);
        }
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="loginDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Login(RequestLoginDTO loginDTO)
        {
            var list=service.Login(loginDTO);
            return Ok(list);
        }
        #region 商品
        /// <summary>
        /// 商品列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="listcount"></param>
        /// <param name="pagecount"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetGoods(int pageindex, int pagesize)
        {
            int listcount = 0;
            int pagecount = 0;
            var list = service.GetGoods(pageindex, pagesize,out listcount, out pagecount);
            return Ok(new {list,listcount,pagecount});
        }
        /// <summary>
        /// 商品添加
        /// </summary>
        /// <param name="good"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddGoods(T_Good good)
        {
            good.GoodId=YitIdHelper.NextId().ToString();
            good.GoodCreateTime = DateTime.Now.ToString();
          return Ok(service.AddGoods(good));
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult LoadImages(IFormFile file)
        {
            var filename=file.FileName;
            filename=YitIdHelper.NextId().ToString()+Path.GetExtension(filename).ToLower();
            var path=Directory.GetCurrentDirectory()+@"/wwwroot/Images/"+filename;
            using(FileStream fs=new FileStream(path,FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            var url="/Images/"+ filename;
            return Ok(url);
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeletedGood(List<T_Good> goods)
        {
            var list = service.DeletedGood(goods);
            return Ok(list);
        }
        /// <summary>
        /// 修改商品
        /// </summary>
        /// <param name="good"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateGood(T_Good good)
        {
           var n=service.UpdateGood(good);
            return Ok(n);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownLoadExcel()
        {
            int listcount = 0;
            int pagecount = 0;
            var list = service.GetGoods(pageindex: 1, 10000, out listcount, out pagecount);
            MemoryStream stream = new MemoryStream();
            stream.SaveAs(list);
            stream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName="商品溯源导出数据.xlsx"
            };
        }
        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult LoadAddGood(IFormFile file)
        {
            if(file==null)
            {
                return BadRequest("文件不能为空");
            }
            var fileName=Path.GetExtension(file.FileName);
            if(fileName!=".xlsx")
            {
                return BadRequest("文件格式不正确");
            }
            MemoryStream stream = new MemoryStream();
            file.CopyTo(stream);
            var list = stream.Query<T_Good>().ToList();
            list.ForEach(c =>
            {
                c.GoodId = YitIdHelper.NextId().ToString();
                c.GoodCreateTime = DateTime.Now.ToString();
                c.GoodUpdatePeople = "";
            });
            service.AddRangeGood(list);

            return Ok(list);
        }
        /// <summary>
        /// 添加图片到图片表
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Url"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddImage(string Id, string Url)
        {
           var list=service.AddImage(Id, Url);
            return Ok(list);
        }
        /// <summary>
        /// 删除图片表的图片
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DeletedImages(string Url)
        {
            var list = service.DeletedImages(Url);
            return Ok(list);
        }
        /// <summary>
        /// 获取图片反填
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetT_Images(string ID)
        {
            var list = service.GetT_Images(ID);
            return Ok(list);
        }

        #endregion
        #region 养殖
        /// <summary>
        /// 养殖信息表添加
        /// </summary>
        /// <param name="cultivation"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddCultivation(T_Cultivation cultivation)
        {
            cultivation.CultivationId=YitIdHelper.NextId().ToString();
            var n=service.AddCultivation(cultivation);
            return Ok(n);
        }
        /// <summary>
        /// 养殖信息表修改
        /// </summary>
        /// <param name="cultivation"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateCultivation(T_Cultivation cultivation)
        {
           var n=service.UpdateCultivation(cultivation);
            return Ok(n);
        }
        /// <summary>
        /// 养殖信息表反填
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCultivationFind(string goodId)
        {
            var list = service.GetCultivationFind(goodId);
            //if(list!=null)
            //{
            //    list = list.CultivationTime.ToString("yyyy年MM月dd");
            //}
            return Ok(list);
        }
        /// <summary>
        /// 显示保健信息
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetT_Immunes(string goodId)
        {
            var list=service.GetT_Immunes(goodId);
            return Ok(list);

        }
        /// <summary>
        /// 添加保健信息
        /// </summary>
        /// <param name="immune"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddUmmunes(T_Immune immune)
        {
           immune.ImmuneId=YitIdHelper.NextId().ToString();
            var list=service.AddImmunes(immune);
            return Ok(list);
        }
        /// <summary>
        /// 修改保健信息
        /// </summary>
        /// <param name="immune"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateImmunes(T_Immune immune)
        {
            var n= service.UpdateImmunes(immune);
            return Ok(n);
        }
        /// <summary>
        /// 删除保健信息
        /// </summary>
        /// <param name="immune"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeletedImmunes(T_Immune immune)
        {
            var n = service.DeletedImmunes(immune);
            return Ok(n);
        }
        /// <summary>
        /// 添加养殖企业
        /// </summary>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddCultivationEnterprise(T_CultivationEnterprise enterprise)
        {
            enterprise.CultivationEnterpriseId=YitIdHelper.NextId().ToString();
            var n=service.AddCultivationEnterprise(enterprise);
            return Ok(n);
        }
        /// <summary>
        /// 修改养殖企业
        /// </summary>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateCultivationEnterprise(T_CultivationEnterprise enterprise)
        {
            var n = service.UpdateCultivationEnterprise(enterprise);
            return Ok(n);
        }
        /// <summary>
        /// 养殖企业反填
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCultivationEnterpriseFind(string goodId)
        {
            var list = service.GetCultivationEnterpriseFind(goodId);
            return Ok(list);
        }
        #endregion
        #region 屠宰
        /// <summary>
        /// 添加屠宰信息
        /// </summary>
        /// <param name="slaughter"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddSlaughter(T_Slaughter slaughter)
        {
           slaughter.SlaughterId=YitIdHelper.NextId().ToString();
            var n=service.AddSlaughter(slaughter);
            return Ok(n);
        }
        /// <summary>
        /// 修改屠宰信息
        /// </summary>
        /// <param name="slaughter"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateSlaughter(T_Slaughter slaughter)
        {
            var n = service.UpdateSlaughter(slaughter);
            return Ok(n);
        }

        /// <summary>
        /// 反填屠宰信息
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSlaughterFind(string goodId)
        {
            var list = service.GetSlaughterFind(goodId);
            return Ok(list);
        }
        /// <summary>
        /// 添加屠宰企业
        /// </summary>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddSlaughterEnterprise(T_SlaughterEnterprise enterprise)
        {
            enterprise.SlaughterEnterpriseId=YitIdHelper.NextId().ToString();
            var n = service.AddSlaughterEnterprise(enterprise);
            return Ok(n);
        }
        /// <summary>
        /// 修改屠宰企业
        /// </summary>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateSlaughterEnterprise(T_SlaughterEnterprise enterprise)
        {
            var n = service.UpdateSlaughterEnterprise(enterprise);
            return Ok(n);
        }
        /// <summary>
        /// 反填屠宰企业
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSlaughterEnterpriseFind(string goodId)
        {
            var n = service.GetSlaughterEnterpriseFind(goodId);
            return Ok(n);
        }
        #endregion
        #region 加工
        /// <summary>
        /// 添加加工
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddProcess(T_Process process)
        {
            process.ProcessId=YitIdHelper.NextId().ToString();
            var n = service.AddProcess(process);
            return Ok(n);
        }
        /// <summary>
        /// 修改加工信息
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateProcess(T_Process process)
        {
            var n = service.UpdateProcess(process);
            return Ok(n); 
        }
        /// <summary>
        /// 反填加工信息
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetProcessFind(string goodId)
        {
            var n = service.GetProcessFind(goodId);
            return Ok(n);
        }
        /// <summary>
        /// 添加加工企业
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddProcessEnterprise(T_ProcessEnterprise process)
        {
            process.ProcessEnterpriseId=YitIdHelper.NextId().ToString();
            var n = service.AddProcessEnterprise(process);
            return Ok(n);
        }
        /// <summary>
        /// 修改加工企业
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateProcessEnterprise(T_ProcessEnterprise process)
        {
            var n = service.UpdateProcessEnterprise(process);
            return Ok(n);
        }
        /// <summary>
        /// 反填加工企业
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetProcessEnterpriseFind(string goodId)
        {
            var n = service.GetProcessEnterpriseFind(goodId);
            return Ok(n);
        }
        /// <summary>
        /// 添加供应商
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddSupplier(T_Supplier supplier)
        {
            supplier.SupplierId=YitIdHelper.NextId().ToString();
            var n = service.AddSupplier(supplier);
            return Ok(n);
        }
        /// <summary>
        /// 修改供应商
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateSupplier(T_Supplier supplier)
        {
            var n = service.UpdateSupplier(supplier);
            return Ok(n);
        }
        /// <summary>
        /// 反填供应商
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSupplierFind(string goodId)
        {
            var list = service.GetSupplierFind(goodId);
            return Ok(list);
        }

        #endregion

    }
}
